/**
 *
 */
package com.fortune.rainshinesystem.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.CustomerModel;
import com.fortune.rainshinecommon.util.StringUtil;
import com.fortune.rainshinesystem.mapper.CustomerMapper;
import com.fortune.rainshinesystem.service.CustomerService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author Administrator
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    private static final Logger logger = LoggerFactory.getLogger(CustomerServiceImpl.class);
    @Autowired
    private CustomerMapper mapper;

    @Override
    public MsgModel add(CustomerModel model) {
        try {
            CustomerModel customerModel = mapper.selectByPhone(model.getCustomerPhone());
            if(customerModel != null){
                return new MsgModel("手机号码已存在！", ResultCode.FAILURE);
            }
            model.setCreateId(model.getUser().getUserId());
            model.setUpdateId(model.getUser().getUserId());
            //顾客编码
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            model.setCustomerCode(uuid);
            int res = mapper.insert(model);
            if (res > 0) {
                return new MsgModel("添加顾客成功！", ResultCode.SUCCESS);
            } else {
                return new MsgModel("添加顾客失败！", ResultCode.FAILURE);
            }
        } catch (Exception e) {
            logger.error("add ====> {}", e);
            return new MsgModel("添加顾客功能异常！", ResultCode.FAILURE);
        }
    }

    @Override
    public MsgModel update(CustomerModel model) {
        try {
        	CustomerModel customerOld = mapper.selectById(model.getId());
        	if(!model.getCustomerPhone().equals(customerOld.getCustomerPhone())) {
        		CustomerModel customerModel = mapper.selectByPhone(model.getCustomerPhone());
                if(customerModel != null){
                    return new MsgModel("手机号码已存在！", ResultCode.FAILURE);
                }
        	}
            model.setUpdateId(model.getUser().getUserId());
            int res = mapper.updateAllColumnById(model);
            if (res > 0) {
                return new MsgModel("更新成功！", ResultCode.SUCCESS);
            } else {
                return new MsgModel("更新失败！", ResultCode.FAILURE);
            }
        } catch (Exception e) {
            logger.error("update ====> {}", e);
            return new MsgModel("更新失败！", ResultCode.FAILURE);
        }
    }

    @Override
    public MsgModel list(ParmsModel parm) {
        try {
            Wrapper<CustomerModel> wrapper = new EntityWrapper<>();
            if (StringUtil.isNotBlank(parm.getSearchKey())) {
                wrapper.like(parm.getSearchKey(), parm.getSearchValue());
            }else{
                //app端
                if(StringUtils.isNotBlank(parm.getSearchValue())){
                    wrapper.like("customer_name", parm.getSearchValue()).or().like("customer_phone", parm.getSearchValue());
                }
            }
            if(StringUtils.isNotBlank(parm.getName())) {
            	wrapper.like("customer_name", parm.getName());
            }
            if(StringUtils.isNotBlank(parm.getPhone())) {
            	wrapper.like("customer_phone", parm.getPhone());
            }
            if(parm.getShopId() != null && parm.getRoleId()== null) {
            	wrapper.and().eq("shop_id", parm.getShopId());
            }
            Page<CustomerModel> page = new Page<>(parm.getPage(), parm.getLimit());
            List<CustomerModel> list = mapper.selectPage(page, wrapper.orderBy("create_time", false));
            return new MsgModel(page.getTotal(), list);
        } catch (Exception e) {
            logger.error("list ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
    }

    @Override
    public MsgModel del(CustomerModel model) {
        try {
            CustomerModel cm = mapper.selectById(model.getId());
            cm.setCustomerStatus((byte) 0);
            mapper.updateById(cm);
            return new MsgModel("删除成功！", ResultCode.SUCCESS);
//            boolean rs = mapper.deleteById(model.getId()) > 0;
//            if (rs) {
//                return new MsgModel("删除成功！", ResultCode.SUCCESS);
//            } else {
//                return new MsgModel("删除失败！", ResultCode.FAILURE);
//            }
        } catch (Exception e) {
            logger.error("del ====> {}", e);
            return new MsgModel("删除失败！", ResultCode.FAILURE);
        }
    }

    @Override
    public CustomerModel getCustomer(Integer customerId) {
        return mapper.selectById(customerId);
    }

    @Override
    public CustomerModel getByCode(String customerCode) {
        return mapper.getByCode(customerCode);
    }

	@Override
	public MsgModel back(CustomerModel model) {
		 try {
			    CustomerModel cm = mapper.selectById(model.getId());
			    CustomerModel customerModel = mapper.selectByPhone(cm.getCustomerPhone());
	            if(customerModel != null){
	                return new MsgModel("手机号码已存在！", ResultCode.FAILURE);
	            }
	            cm.setCustomerStatus((byte) 1);
	            mapper.updateById(cm);
	            return new MsgModel("恢复成功！", ResultCode.SUCCESS);
	        } catch (Exception e) {
	            logger.error("del ====> {}", e);
	            return new MsgModel("恢复失败！", ResultCode.FAILURE);
	        }
	}

	@Override
	public MsgModel listActive(ParmsModel parm) {
        try {
            Wrapper<CustomerModel> wrapper = new EntityWrapper<>();
            if (StringUtil.isNotBlank(parm.getSearchKey())) {
                wrapper.like(parm.getSearchKey(), parm.getSearchValue());
            }
            if(StringUtils.isNotBlank(parm.getName())) {
            	wrapper.like("customer_name", parm.getName());
            }
            if(StringUtils.isNotBlank(parm.getPhone())) {
            	wrapper.like("customer_phone", parm.getPhone());
            }
            if(parm.getShopId() != null && parm.getRoleId()== null) {
            	wrapper.and().eq("shop_id", parm.getShopId());
            }
            wrapper.eq("customer_status", 1);
            Page<CustomerModel> page = new Page<>(parm.getPage(), parm.getLimit());
            List<CustomerModel> list = mapper.selectPage(page, wrapper.orderBy("create_time", false));
            return new MsgModel(page.getTotal(), list);
        } catch (Exception e) {
            logger.error("list ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
	}

}
